package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class VerifyCodeController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/initVerifyCodeImg")
    public void initVerifyCodeImg(String uuid, HttpServletResponse response) throws IOException {
        //生成验证码
        String exp = VerifyCodeImgUtil.generateVerifyCode();
        //计算结果
        Integer result = VerifyCodeImgUtil.calc(exp);
        //放入到redis中
        redisService.set(SeckillKeyPrefix.SECKILL_VERIFY_CODE,uuid,result);
        //生成图片 响应给浏览器
        BufferedImage bufferedImage = VerifyCodeImgUtil.createVerifyCodeImg(exp);
        ImageIO.write(bufferedImage,"JPG",response.getOutputStream());
    }
}